<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/27 0027
 * Time: 9:02
 */

namespace member\permission;

use member\permission;

/**
 * Class admin
 * @package member\permission
 */
class admin extends permission
{
    /**
     * admin constructor.
     * @param $mem_info
     */
    function __construct($mem_info)
    {
        $this->id_roles = $mem_info['id_roles']; // 会员所属角色组
        $this->mid = $mem_info['id']; // 会员ID
    }

    /**
     * 列出当前会员有权的后台节点
     */
    public function list_nodes()
    {
        $dao_permission = db('Permission');
        $code_nodes_permission = $dao_permission->where([
            'id_role' => ['in', $this->id_roles],
            'no_permission' => 0, // 取角色集有权节点集
        ])->column('code_node');
        $code_nodes_no_permission = $dao_permission->where([
            'id_role' => ['in', $this->id_roles],
            'no_permission' => 1, // 取角色集无权节点集
        ])->column('code_node');
        $code_nodes_no_permission_mid = $dao_permission->where([
            'mid' => $this->mid,
            'no_permission' => 1, // 取会员无权节点
        ])->column('code_node');
        // 若a角色对节点无权限, b角色对节点有权限, 则为有权限 (所以当节点在无权组, 却不在有权组, 则为无权)
        $code_nodes_no_permission = array_diff($code_nodes_no_permission, $code_nodes_permission);
        // 若会员对节点无权限, 则无权限
        $map_code_nodes_no_permission = array_flip(array_merge($code_nodes_no_permission, $code_nodes_no_permission_mid));
        $nodes = parent::list_nodes();
        $nodes_code_key = [];
        foreach ($nodes as $node) { // 取有权节点集
            if (array_key_exists($node['code'], $map_code_nodes_no_permission)) continue;
            $nodes_code_key[$node['code']] = $node;
        }
        return $nodes_code_key;
    }
}